#!/bin/bash
set -euo pipefail
IFS=$'\n\t'

function openBrowser
{
    GITHUBURL=$(gh browse --no-browser --branch $(git branch --show-current))
    open $GITHUBURL
}

function openCodespace
{
    GITHUBURL=$(gh browse --no-browser --branch $(git branch --show-current))
    CODESPACESURL="https://github.dev/${GITHUBURL/https:\/\/github.com//}"
    CODESPACESURL="${CODESPACESURL/\/\/\///}"
    open $CODESPACESURL
}

function openGitPod
{
    GITHUBURL=$(gh browse --no-browser --branch $(git branch --show-current))
    CODESPACESURL="https://gitpod.io/#${GITHUBURL}"
    CODESPACESURL="${CODESPACESURL/\/\/\///}"
    open $CODESPACESURL
}

function openVscode
{
    GITHUBURL=$(gh browse --no-browser --branch $(git branch --show-current))
    CODESPACESURL="vscode://vscode.git/clone?url=${GITHUBURL}"
    CODESPACESURL="${CODESPACESURL/\/\/\///}"
    powershell.exe start $CODESPACESURL
}

function openIdea
{
    GITHUBURL=$(gh browse --no-browser --branch $(git branch --show-current))
    CODESPACESURL="jetbrains://idea/checkout/git?checkout.repo=${GITHUBURL}"
    CODESPACESURL="${CODESPACESURL/\/\/\///}"
    powershell.exe start "$CODESPACESURL"
}

function mrCheckout
{
    git fetch origin merge-requests/$1/head:mr-$1
    git checkout mr-$1
}

function open
{
    if [[ $(uname -r)  == *"WSL2" ]]; then
        powershell.exe start "$1"
    elif [[ "$OSTYPE" == "linux-gnu"* ]]; then
        sensible-browser "${1}" || x-www-browser "${1}" ||  xdg-open "${1}" || gnome-open "${1}"
    elif [[ "$OSTYPE" == "darwin"* ]]; then
        open "${1}"
    else
        echo Error: unknown operating system, unable to open "${1}"
        exit 1
    fi
}

function mailConfig
{
    git config --local user.name 'rick'
    git config --local user.email 'LinuxSuRen@users.noreply.github.com'
}

function clone
{
    git clone https://gh-proxy.com/https://github.com/$1 $1
    cd $1
    git remote set-url origin https://github.com/$1
    gh repo fork --remote
}

function push
{
    watch --chgexit -n 3 "timeout 3s git push $@"
}

function pull
{
    watch --chgexit -n 3 "timeout 3s git pull"
}

function help
{
    cat << EOF
Enhancement of GitHub Command line interface
Usage:
  gh dev [command]
Available Commands:
  help                Help about any command
  open                Opens the git repository in a browser
  codespace           Opens the GitHub repository in a workspace
  gitpod              Opens the GitHub repository in a GitPod workspace
  mail                Set the git config (name and email) locally
  mrCheckout          Checkout to a MR in a Gitlab repository
  clone               Clone and fork the repository from a mirror of GitHub
  push                Run 'git push' command for many times
  pull                Run 'git pull' command for many times
Flags:
  -h, --help   help for gh dev
EOF
}

while [ $# -gt 0 ]; do
  case "$1" in
  open|--open)
    openBrowser
    exit 0
    ;;
  codespace|cs|--codespace|--cs)
    openCodespace
    exit 0
    ;;
  gitpod|--gitpod)
    openGitPod
    exit 0
    ;;
  vscode|--vscode)
    openVscode
    exit 0
    ;;
  idea|--idea)
    openIdea
    exit 0
    ;;
  mrCheckout|mrCo|--mrCheckout|--mrCo)
    mrCheckout $2
    exit 0
    ;;
  mail|--mail)
    mailConfig
    exit 0
    ;;
  clone|--clone)
    clone $2
    exit 0
    ;;
  push|--push)
    shift
    push $@
    exit 0
    ;;
  pull|--pull)
    pull
    exit 0
    ;;
  -h|--help)
    help
    exit 0
    ;;
    *)
    cat << EOF
Error: unknown command "$1" for "gh dev"
Run 'gh dev --help' for usage.
EOF
  exit 1
  ;;
  esac
done

# default behaviour is to print the help information
help
